package com.vitae.controller.filter;

import com.vitae.common.DomainUrlUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Set;

/**
 * Created by Administrator on 2018/5/15.
 */
public class AccessFilter implements Filter {
    private Set<String> excludeUrls;
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request=(HttpServletRequest)req;
        HttpServletResponse response=(HttpServletResponse)resp;
        //获取访问者ip
       String ip= request.getRemoteHost();
       //获取访问连接
        String url =request.getRequestURI();
        HttpSession session;
        try {
            // 放行例外连接地址
//            if (null != this.excludeUrls && !this.excludeUrls.isEmpty()) {
//                String tempURI = -1 != uri.indexOf("?") ? uri.substring(0, uri.indexOf("?")) : uri;
//                if (this.excludeUrls.contains(tempURI)) {
//                    filterChain.doFilter(request, response);
//                    return;
//                }
//            }
            if(request.getSession(false) == null){
                session= request.getSession(true);
                if(session.getAttribute("user")==null ){
                    session.setAttribute("userId",1);
                }
                session.setAttribute("domainUrl", DomainUrlUtil.getEJS_URL_RESOURCES());
                session.setAttribute("domainStaticUrl", DomainUrlUtil.getEJS_STATIC_RESOURCES());
            }
            chain.doFilter(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void init(FilterConfig config) throws ServletException {
        System.out.println(config);
    }

}
